#pragma once

#include <string>
#include <vector>
#include <fstream>
#include "Log.h"

#define PATJNAME "./dirct.txt"
#define FORMAT ": "

class direct
{

    using data_type = std::pair<std::string, std::string>;

public:
    direct()
    {
        std::ifstream fin;
        fin.open(PATJNAME, std::ios_base::in);
        if (!fin.is_open())
        {
            LOG(zmh::LogLevel::ERROR) << "open mistake";
            return;
        }

        std::string buffer;
        while (std::getline(fin, buffer))
        {
            int pos = buffer.find(FORMAT); // 查找字串,查找成功,返回该字串的第一个位置
            if (pos == std::string::npos)
            {
                LOG(zmh::LogLevel::INFO) << "该行格式不正确,已过滤 buffer为" << (buffer.size() == 0 ? "(空白行)" : buffer);
                continue;
            }
            const std::string &English = buffer.substr(0, pos);
            const std::string &Chinese = buffer.substr(pos + strlen(FORMAT));
            if (English.size() == 0 || Chinese.size() == 0)
            {
                LOG(zmh::LogLevel::INFO) << "该行格式不正确,缺中文或英文,已过滤" << buffer;
                continue;
            }
            _hash.emplace_back(English, Chinese);
        }
        fin.close();
    }

    std::string Translation(std::string mes) // 英文 -> 中文
    {
        for (const auto &[en, ch] : _hash)
        {
            if(ch == mes)
                return en;
            else if(en == mes)
                return ch;
        }
        return "您好！没有找到匹配的选项";
    }

private:
    std::vector<data_type> _hash;
};
